package com.example.mypic;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

import com.example.mypic.adapters.PicCursorAdapter;
import com.example.mypic.models.PicHelper;
import com.example.mypic.utils.Utils;

public class RecentActivity extends ListActivity {
	private PicCursorAdapter adapter;
	private PicHelper helper;
	private ProgressDialog dialog;
	private Cursor c;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		helper = new PicHelper(this);
		runOnUiThread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				c = helper.getAll();

				adapter = new PicCursorAdapter(RecentActivity.this, c, helper);
				setListAdapter(adapter);
			}
		});
	}

	@Override
	protected void onListItemClick(ListView l, View v, final int position,
			long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		//
		// Toast.makeText(getApplicationContext(), position + "",
		// Toast.LENGTH_SHORT).show();
		c.moveToPosition(position);
		runOnUiThread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub

				Cursor cursor = helper.getByImageID(helper.getImageID(c));
				if (cursor.getCount() <= 0) {
					// helper.insert(models.get(position));
				} else {
					cursor.moveToFirst();
					helper.update2(helper.getImageID(cursor),
							System.currentTimeMillis());
				}

			}
		});

		if (Utils.recent.containsKey(helper.getImageID(c))) {
			Intent intent = new Intent("android.intent.action.DETAIL");
			intent.putExtra(Utils.KEY, helper.getImageID(c));
			startActivity(intent);
		} else {
			dialog = ProgressDialog.show(RecentActivity.this, "", "Loading...");
			new RecentTask().execute(position);
		}
	}

	private class RecentTask extends AsyncTask<Integer, Void, Boolean> {
		private int position;

		@Override
		protected Boolean doInBackground(Integer... params) {
			// TODO Auto-generated method stub
			position = params[0];
			c.moveToPosition(position);
			// IPWallpaperImage wall = models.get(position);
			String linkImage = helper.getImage(c);
			try {
				InputStream is = (InputStream) new URL(linkImage).getContent();
				Drawable drawable = Drawable.createFromStream(is, "iamge_"
						+ helper.getImageID(c));

				Utils.recent.put(helper.getImageID(c), drawable);
				return true;
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return false;
		}

		@Override
		protected void onPostExecute(Boolean result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			if (dialog != null) {
				dialog.dismiss();
			}
			if (result) {
				Intent intent = new Intent("android.intent.action.DETAIL");
				intent.putExtra(Utils.KEY, helper.getImageID(c));
				startActivity(intent);
			} else {
				Toast.makeText(RecentActivity.this, "Không thể tải ảnh",
						Toast.LENGTH_SHORT).show();
			}
		}
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		if (helper != null) {
			helper.close();
		}
		super.onPause();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		if (helper == null) {
			helper = new PicHelper(this);
		}
		super.onResume();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		if (helper != null) {
			helper.close();
		}
		super.onDestroy();
	}
}
